<?
class ImageManipulator {
	
	//----------------------
	//decider(): Manage image depending on type
	//----------------------
	function decider($image_type, $file) {
		$image_type = strtolower($image_type);
		switch($image_type) {
			case 'jpg': $this -> doItJpeg($file); break;
			case 'jpeg': $this -> doItJpeg($file); break;
			case 'png': $this -> doItPng($file); break;
			case 'gif': $this -> doItGif($file); break;
		}
	}
	
	//----------------------
	//doItJpeg(): resize the image
	//----------------------
	function doItJpeg($file) {
	
		$msg = "1"; //initially, assume all is well
    
		$size = GetImageSize($file); //get width/height  (path support from PHP 4.05)
		$image = @imagecreatefromjpeg($file); //attempt to open original image
		
		//resize only if bigger than the allowed image size
		if($size[0]>IMAGE_WIDTH || $size[1]>IMAGE_HEIGHT) {
      		
			if($size[0]>$size[1]) {
				$img_width=IMAGE_WIDTH;
				$img_height=intval((IMAGE_WIDTH*$size[1])/$size[0]);
			} else {
				$img_width=intval((IMAGE_HEIGHT*$size[0])/$size[1]);
				$img_height=IMAGE_HEIGHT;
			}

		} else {
			$img_width=$size[0];
			$img_height=$size[1];
		}
		
		if($size[0]>$size[1]) {
			$thumb_width=THUMB_WIDTH;
			$thumb_height=intval((THUMB_WIDTH*$size[1])/$size[0]);
		} else {
			$thumb_width=intval((THUMB_HEIGHT*$size[0])/$size[1]);
			$thumb_height=THUMB_HEIGHT;
		}
		
		$resized_image = imagecreatetruecolor($img_width,$img_height);
		@ImageCopyResampled($resized_image, $image, 0, 0, 0, 0, $img_width, $img_height, $size[0], $size[1]); //resize and resample image

    	$thumbnail = imagecreatetruecolor($thumb_width,$thumb_height);
		@ImageCopyResampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]); //resize and resample thumbnail
    		
		@ImageDestroy($image); //free memory
		if (!@ImageJPEG($resized_image, $file.'_image')) { // try to save resized image
			$msg .= 'Cannot create '.$file.' JPEG file';
		}
		
    	if (!@ImageJPEG($thumbnail, $file.'_thumb')) { // try to save thumbnail image
			$msg .= 'Cannot create '.$file.' thumb JPEG file';
		}

		return $msg;
	}
	
	//----------------------
	//doItGif(): resize the image
	//----------------------
	function doItGif($file) {
		
		$msg = "1"; //initially, assume all is well
		
		$size = GetImageSize($file); //get width/height  (path support from PHP 4.05)
		$image = imagecreatefromgif($file); //attempt to open original image
		
		//resize only if bigger than the allowed image size
		if($size[0]>IMAGE_WIDTH) {
      		
			if($size[0]>$size[1]) {
				$img_width=IMAGE_WIDTH;
				$img_height=intval((IMAGE_WIDTH*$size[1])/$size[0]);
			} else {
				$img_width=intval((IMAGE_HEIGHT*$size[0])/$size[1]);
				$img_height=IMAGE_HEIGHT;
			}

		} else {
			$img_width=$size[0];
			$img_height=$size[1];
		}
		
		if($size[0]>$size[1]) {
			$thumb_width=THUMB_WIDTH;
			$thumb_height=intval((THUMB_WIDTH*$size[1])/$size[0]);
		} else {
			$thumb_width=intval((THUMB_HEIGHT*$size[0])/$size[1]);
			$thumb_height=THUMB_HEIGHT;
		}
		
		$resized_image = imagecreatetruecolor($img_width,$img_height);
		@ImageCopyResampled($resized_image, $image, 0, 0, 0, 0, $img_width, $img_height, $size[0], $size[1]); //resize and resample image

    	$thumbnail = imagecreatetruecolor($thumb_width,$thumb_height);
		@ImageCopyResampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]); //resize and resample thumbnail
		
		@ImageDestroy($image); //free memory
		if (!@ImageGIF($resized_image, $file.'_image')) { // try to save thumbnail image
			$msg .= 'Cannot create '.$file.' GIF file';
		}
		if (!@ImageGIF($thumbnail, $file.'_thumb')) { // try to save thumbnail image
			$msg .= 'Cannot create '.$file.' thumb GIF file';
		}
			
		return $msg;
	}

	//----------------------
	//doItPng(): resize the image
	//----------------------
	function doItPng($file) {
		$msg = "1"; //initially, assume all is well

		$size = GetImageSize($file); //get width/height  (path support from PHP 4.05)
		$image = @imagecreatefrompng($file); //attempt to open original image
		
		//resize only if bigger than the allowed image size
		if($size[0]>IMAGE_WIDTH) {
      		
			if($size[0]>$size[1]) {
				$img_width=IMAGE_WIDTH;
				$img_height=intval((IMAGE_WIDTH*$size[1])/$size[0]);
			} else {
				$img_width=intval((IMAGE_HEIGHT*$size[0])/$size[1]);
				$img_height=IMAGE_HEIGHT;
			}

		} else {
			$img_width=$size[0];
			$img_height=$size[1];
		}
		
		if($size[0]>$size[1]) {
			$thumb_width=THUMB_WIDTH;
			$thumb_height=intval((THUMB_WIDTH*$size[1])/$size[0]);
		} else {
			$thumb_width=intval((THUMB_HEIGHT*$size[0])/$size[1]);
			$thumb_height=THUMB_HEIGHT;
		}
		
		$resized_image = imagecreatetruecolor($img_width,$img_height);
		@ImageCopyResampled($resized_image, $image, 0, 0, 0, 0, $img_width, $img_height, $size[0], $size[1]); //resize and resample image

    	$thumbnail = imagecreatetruecolor($thumb_width,$thumb_height);
		@ImageCopyResampled($thumbnail, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]); //resize and resample thumbnail

		@ImageDestroy($image); //free memory
    	if (!@ImagePNG($resized_image, $file.'_image')) { // try to save thumbnail image
			$msg .= 'Cannot create '.$file.' PNG file';
		}
		if (!@ImagePNG($thumbnail, $file.'_thumb')) { // try to save thumbnail image
			$msg .= 'Cannot create '.$file.' thumb PNG file';
		}
			
		return $msg;
	}

}//end class

?>